﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using Microsoft.Win32;

namespace MemoryLeakViewerLib
{
    public partial class CodeForm : Form
    {
        public CodeForm()
        {
            InitializeComponent();
        }

        private void CodeForm_Load(object sender, EventArgs e)
        {
            label1.Visible = false;
            fileLabel.Text = "";
        }

        public void SetText(string i_fullPath, int i_line)
        {
            this.SuspendLayout();

            //this.Text = "Code View (" + i_fullPath + ", Line: " + i_line + ")";
            fileLabel.Text = i_fullPath + ", Line: " + i_line;

            bool makeLabel1Visible = false;
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(i_fullPath); //Program.m_ReportFileInfo
            if (!fileInfo.Exists)
            {
                richTextBox1.Clear();
                //label1.Visible = true;
                makeLabel1Visible = true;
                label1.Text = "ERROR: The specified file does not exist.";
                //richTextBox1.Text = "ERROR: The specified file does not exist.";
                this.ResumeLayout();
                label1.Visible = makeLabel1Visible;
                return;
            }

            if (fileInfo.LastWriteTime > Program.m_ReportFileInfo.LastWriteTime)
            {
                //label1.Visible = true;
                makeLabel1Visible = true;
                label1.Text = "WARNING: This file has been modified since the memory leak report was generated!";
                //richTextBox1.Clear();
                //richTextBox1.Text = "WARNING: This file has been updated since this memory leak report was generated!";
                //return;
            }

            // lines are 1 based in Visual Studio
            // they are 0 based here.
            i_line -= 1;

            string[] lines;
            try
            {
                lines = System.IO.File.ReadAllLines(i_fullPath);
            }
            catch
            {
                richTextBox1.Clear();
                richTextBox1.Text = "ERROR: Could not open the specified file.";
                this.ResumeLayout();
                return;    
            }
            //string text = System.IO.File.ReadAllText(i_fullPath);

            for (int i = 0; i < lines.Length; i++)
            {
                //string lineString = String.Format("{0,10}", i.ToString());
                //string lineString = (i + 1) + ":   ";
                string lineString = "";
                lines[i] = lineString + lines[i];
            }

            richTextBox1.SuspendLayout();
            //richTextBox1.Text = text;
            richTextBox1.Lines = lines;

            if (richTextBox1.Lines.Length > i_line && richTextBox1.Lines[i_line].Length > 0)
            {
                string DEBUG_line = richTextBox1.Lines[i_line];

                int first = richTextBox1.GetFirstCharIndexFromLine(i_line);
                richTextBox1.SelectionStart = first;
                richTextBox1.SelectionLength = richTextBox1.Lines[i_line].Length;

                richTextBox1.SelectionColor = Color.Red;
                //richTextBox1.SelectionBackColor = Color.Gainsboro;
                richTextBox1.SelectionLength = 0;

                // set the scroll position so that the line appears in the middle of the screen.
                int numLines = richTextBox1.Height / richTextBox1.Font.Height;
                int newStart = i_line - (numLines/2);
                if (newStart < 0)
                    newStart = 0;

                richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(newStart);
                richTextBox1.ScrollToCaret();
                richTextBox1.SelectionLength = 0;

                richTextBox1.SelectionStart = first;
            }
            else
            {
                richTextBox1.Clear();
                richTextBox1.Text = "ERROR: The requested line was not found or was blank. Your memory leak log may be out of date.";
            }

            richTextBox1.ResumeLayout();

            label1.Visible = makeLabel1Visible;
            this.ResumeLayout();
            //textBox1
        }
    }
}
